package com.utils;

import com.dao.*;
import com.entity.*;
import com.service.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

public class ContentUtil {
    private static UserDao userDao = SpringContextHolder.getBean(UserDao.class);



    public static int getListLength(List list) {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }


    public static boolean inArray(String[] array, String val) {
        if (array == null || val == null) {
            return false;
        }

        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(val)) {
                return true;
            }
        }
        return false;
    }



    /**
     * @return
     * @Author
     * @Description 根据用户id 获取用户
     * @Param
     **/
    public static User getUserById(String id) {
        if (StringUtils.isNotBlank(id)) {
            User user = userDao.getModel(id);
            if (user != null) {
                return user;
            }
        }
        return new User();
    }
    /**
     * @return
     * @Author
     * @Description 根据用户id 获取用户
     * @Param
     **/
    public static String getNameById(String id) {
        if (StringUtils.isNotBlank(id)) {
            User user = userDao.getModel(id);
            if (user != null) {
                return user.getName();
            }
        }
        return "";
    }

    /**
     * @return
     * @Author
     * @Description 将金额改成千分位分隔符
     * @Param
     **/
    public static String parseNumber(String money) {
        if ("0".equals(money)) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat(",###,###.00");
        return df.format(new BigDecimal(money));
    }




    /**
     * @return 1天前 2小时前 50分钟前 1分钟前
     * @Author
     * @Description 计算出日期距离当前时间多长时间
     * @Param 时间
     **/
    public static String getDatePoor(Date date) {
        Date nowDate = new Date();
        long nd = 1000 * 24 * 60 * 60;//每天毫秒数

        long nh = 1000 * 60 * 60;//每小时毫秒数

        long nm = 1000 * 60;//每分钟毫秒数

        long diff = nowDate.getTime() - date.getTime(); // 获得两个时间的毫秒时间差异

        long day = diff / nd;   // 计算差多少天

        long hour = diff % nd / nh; // 计算差多少小时

        long min = diff % nd % nh / nm;  // 计算差多少分钟

        //10分钟之内输出刚刚
        if (diff < nm * 10) {
            return "刚刚";
        } else if (diff < nh) {
            return min + "分钟前";
        } else if (diff < nd) {
            return hour + "小时前";
        } else {
            return day + "天前";
        }
    }


    public static String submitString(String content,Integer num) {
        if (content.length() > num) {
            String substring = content.substring(0, num);
            return substring + "...";
        }
        return content;
    }

}
